package com.swordget.dao.impl;

import com.swordget.commons.JdbcUtil;
import com.swordget.entity.po.BaseDataMappingPo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: David
 * @Date: Created in 9:35 2021/5/8
 * @Description: 基础数据映射实现
 * @Modified By:
 * @Version:
 */
@Slf4j
@Repository
public class BaseDataMappingDaoImpl {

    @Autowired
    private JdbcUtil jdbcUtil;

    public Map<Integer, List<String>> groupByType() {
        String commend = "SELECT TYPE,COUNT(*) FROM BASEDATA_MAPPING GROUP BY TYPE";
        return jdbcUtil.findAll(commend);
    }

    public Map<Integer, List<String>> findAllByType(String type) {
        String commend = String.format("SELECT CODE,CODEVALUE FROM BASEDATA_MAPPING WHERE TYPE = '%s'", type);
        return jdbcUtil.findAll(commend);
    }

    public int countByType(String typeName) {
        String commend = String.format("SELECT count(*) FROM BASEDATA_MAPPING WHERE TYPE = '%s'", typeName);
        return jdbcUtil.executeCount(commend);
    }

    public boolean save(List<BaseDataMappingPo> mappingPos) {
        List<String> commends = new ArrayList<>();
        String commend = "INSERT INTO BASEDATA_MAPPING(TYPE, CODE, CODEVALUE) VALUES('%s', '%s', '%s')";
        for (BaseDataMappingPo mappingPo : mappingPos) {
            commends.add(String.format(commend, mappingPo.getType(), mappingPo.getCode(), mappingPo.getCodeName()));
        }
        return jdbcUtil.executeBatch(commends);
    }
}
